﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using com.intellitack.layar.library;
using System.Globalization;

namespace com.intellitack.layar.service
{
    public class LayarGetPOIHttpHandler:IHttpHandler
    {
        ILayerApplication _layarApplication;

        #region Construction
        public LayarGetPOIHttpHandler() 
        {
            string targetType = ConfigurationManager.AppSettings.Get("LayarApplicationClass");
            Type layarType = Type.GetType(targetType);
            _layarApplication = Activator.CreateInstance(layarType) as ILayerApplication;
            if (_layarApplication == null) 
            {
                throw new ApplicationException("The type specified in the web.config was not an ILayarApplication");
            }

            
        }
        #endregion

        #region IHttpHandler Members

        public bool IsReusable
        {
            //To enable pooling, return true here.
            //This keeps the handler in memory.
            get { return true; }
        }

        public void ProcessRequest(HttpContext context)
        {
            //Get Request from HttpContext
            LayarRequest request = getRequestFromContext(context);

            //Process Request
            LayarResponse response = _layarApplication.processLayarRequest(request);

            //Write Response
            context.Response.Write(response.Serialize());
        }

        protected LayarRequest getRequestFromContext(HttpContext context) 
        {
            //Validate the Request somehow

            //Instantiate the LayarRequest from the HttpContext
            LayarRequest request = new LayarRequest();

            //Map the Request from the HttpRequest to the Business Object
            request.userId = context.Request.Params.Get("userId");
            request.developerID = context.Request.Params.Get("developerId");
            request.developerHash = context.Request.Params.Get("developerHash");
            request.timestamp = getIntParameter("timestamp", context);
            request.layerName = context.Request.Params.Get("layerName");
            request.lat = getDecimalParameter("lat", context);
            request.lon = getDecimalParameter("lon", context);
            request.accuracy = getIntParameter("accuracy", context);
            request.RADIOLIST = context.Request.Params.Get("RADIOLIST");
            request.SEARCHBOX = context.Request.Params.Get("SEARCHBOX");
            request.radius = getIntParameter("radius", context);
            request.CUSTOM_SLIDER = getDecimalParameter("CUSTOM_SLIDER", context);
            request.pageKey = context.Request.Params.Get("pageKey");
            request.oauth_consumer_key = context.Request.Params.Get("oauth_consumer_key");
            request.oauth_signature_method = context.Request.Params.Get("oauth_signature_method");
            request.oauth_timestamp = getIntParameter("oauth_timestamp", context);
            request.oauth_nonce = context.Request.Params.Get("oauth_nonce");
            request.oauth_version = context.Request.Params.Get("oauth_version");
            request.oauth_signature = context.Request.Params.Get("oauth_signature");

            return request;
        }

        protected decimal? getDecimalParameter(string key, HttpContext context)
        {
            //Create the placeholder type to be sent to TryParse
            decimal tryDecimal;

            NumberStyles style = NumberStyles.Number;
            CultureInfo culture = CultureInfo.CreateSpecificCulture("en-us");

            if (Decimal.TryParse(context.Request.Params.Get(key), style, culture, out tryDecimal))
            {
                return tryDecimal;
            }
            else
            {
                return null;
            }
        }


        protected Int64? getIntParameter(string key, HttpContext context) 
        {
            //Create the placeholder type to be sent to TryParse
            Int64 tryInt;

            if (Int64.TryParse(context.Request.Params.Get(key), out tryInt))
            {
                return tryInt;
            }
            else 
            {
                return null;
            }
        }

        

        private LayarResponse createSampleResponse() 
        {
            
            //Create a sample response.
            LayarResponse response = new LayarResponse();
            response.layer = "interactivejaxtest";
            response.morePages = false;
            return response;
        }

        #endregion
    }
}
